home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3305 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: Rezonet.net!news
  2. From: ray@ultimate-tech.com (Ray Dunn)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Floating point calculation order
  5. Date: 25 Jan 1996 17:09:21 GMT
  6. Organization: Ultimate Technographics Inc.
  7. Message-ID: <4e8dg1$ehg@ns.RezoNet.NET>
  8. References: <m0tedv8-0002eqC@sice.nsk.su> <3104c6d9.134061184@nntp.ix.netcom.com> <DLnE5K.2xH@microunity.com> <822428038snz@genesis.demon.co.uk>
  9. NNTP-Posting-Host: 204.19.230.7
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. X-Newsreader: WinVN 0.99.7
  13.  
  14. In referenced article, Lawrence Kirby says...
  15. >Parentheses in C allow you to change the operator/operand grouping 
  16. >within expressions. They don't enforce any order of evaluation beyond 
  17. >that implied by the grouping. So:
  18. >
  19. > x = p * q / r;
  20. >
  21. >and
  22. >
  23. > x = (p * q) / r;
  24. >
  25. >mean *exactly* the same thing in C since the parentheses don't change 
  26. >the grouping.
  27.  
  28. You're right, of course, but they only mean the same thing because * 
  29. and / have the same precedence and associate left to right.
  30.  
  31. Remember that the original posting was from someone complaining that 
  32. the division was done first (presumably on a non-conforming compiler). 
  33. In this case parenthesizing the multiplication would indeed change the 
  34. order of evaluation and solve his problem.
  35.  
  36. I do think it's a good idea to parenthesize expressions to make it 
  37. clear what you expect to happen.
  38.  
  39. It's probably helpful to use this opportunity to remind beginners that 
  40. although the * is guaranteed to be done before the /, there is no 
  41. definition of which order the *operands* will be evaluated, so for 
  42. example, in:
  43.  
  44.   x = f() * g() / h();
  45.  
  46. the calls of f, g, and h may be made in any order the compiler chooses, 
  47. even if terms are parenthesised and/or operators of different 
  48. precedence are used.
  49. -- 
  50. Ray Dunn (opinions are my own) | Phone: (514) 938 9050
  51. Montreal                       | Phax : (514) 938 5225
  52. ray@ultimate-tech.com          | Home : (514) 630 3749
  53.  
  54.